package cn.wolfcode.luowowo.listener;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.key.RedisTravelKey;
import cn.wolfcode.luowowo.cache.service.ITravelStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.TravelStats;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
/**
 *  初始化热门数据监听器
 *  服务器启动时读取热门数据到redis中
 */
public class HotTravelDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Value("${hotData.init}")
    //标记,决定是否进行热门数据读取
    private boolean flag;
    @Reference
    //引入攻略文章服务
    private ITravelService travelService;
    @Reference
    //引入攻略热门数据缓存服务
    private ITravelStatsCacheService travelStatsCacheService;

    @Override
    //监听器的监听方法实现,创建应用时触发
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        //标记为true时,执行初始化
        if (flag) {
            System.out.println("开始初始化");
            //获取攻略文章列表
            List<Travel> details = travelService.listAll();
            for (Travel detail : details) {
                //判断此文章是否已经缓存在redis中
                if (travelStatsCacheService.notExist(detail.getId())) {
                    //需要进行缓存
                    //攻略统计对象
                    TravelStats stats = new TravelStats();
                    //值迁移
                    stats.setDestId(detail.getDest().getId());
                    stats.setDestName(detail.getDest().getName());
                    stats.setTitle(detail.getTitle());
                    stats.setTravelId(detail.getId());
                    stats.setViewnum(detail.getViewnum());
                    stats.setFavornum(detail.getFavornum());
                    stats.setSharenum(detail.getSharenum());
                    stats.setReplynum(detail.getReplynum());
                    stats.setThumbsupnum(detail.getThumbsupnum());
                    //保存到redis中
                    travelStatsCacheService.save(detail.getId(), stats);
                }
                //排行榜数据初始化
                //飙升
                //传入飙升榜的枚举对象,以及攻略文章的id
                //id用来获取攻略统计对象
                if (travelStatsCacheService.notExistRank(RedisTravelKey.TRAVEL_STATS_UP_SORT, detail.getId())) {
                    //求和
                    int num = detail.getViewnum() + detail.getReplynum();
                    //保存
                    travelStatsCacheService.addRank(RedisTravelKey.TRAVEL_STATS_UP_SORT, num, detail.getId());
                }
                //热门
                if (travelStatsCacheService.notExistRank(RedisTravelKey.TRAVEL_STATS_HOT_SORT, detail.getId())) {
                    //求和
                    int num = detail.getFavornum() + detail.getThumbsupnum();
                    //保存
                    travelStatsCacheService.addRank(RedisTravelKey.TRAVEL_STATS_HOT_SORT, num, detail.getId());

                }
            }
            System.out.println("初始化完成");
        }
    }
}
